//
//  CarDetailVC.m
//  New_Runway
//
//  Created by mack on 2018/1/25.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CarDetailVC.h"
#import <SDCycleScrollView.h>
#import "UITableView+FDTemplateLayoutCell.h"
#import "CarDetailTopCell.h"
#import "CarDetailPrintCell.h"
//#import "CarDetailPriceCell.h"
#import "CarFloorPriceCell.h"
#import "CardescribeCell.h"
#import "CarCodeCell.h"
#import "CarRadarDataCell.h"


@interface CarDetailVC ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate, CarDetailTopCellDelegate>
@property(nonatomic, strong) UITableView *tableview;
@property (nonatomic,strong) SDCycleScrollView *bannerView;
@property (nonatomic, strong) UILabel *bannerL;
@property (nonatomic, assign) BOOL isShowCode;
@property (strong, nonatomic) UIWebView *callPhoneWebView;

@end

@implementation CarDetailVC

- (UIWebView *)callPhoneWebView {
    
    if (_callPhoneWebView == nil) {
        _callPhoneWebView = [[UIWebView alloc] init];
    }
    return _callPhoneWebView;
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.tableview reloadData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[SDImageCache sharedImageCache] setShouldDecompressImages:NO];
    [[SDWebImageDownloader sharedDownloader] setShouldDecompressImages:NO];
}

- (void)dealloc {
    
    [[SDImageCache sharedImageCache] setShouldDecompressImages:YES];
    [[SDWebImageDownloader sharedDownloader] setShouldDecompressImages:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarView.hidden = YES;
    self.isShowCode = NO;
    [self initTableview];
//    [self loadCarBaseInfo];
    

}
-(void)initTableview{
    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.backgroundColor = MAIN_COLOR();
    _tableview.estimatedRowHeight = 0;
    _tableview.estimatedSectionHeaderHeight = 0;
    _tableview.estimatedSectionFooterHeight = 0;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableview registerNib:[UINib nibWithNibName:@"CarDetailTopCell" bundle:nil] forCellReuseIdentifier:@"CarDetailTopCell"];
    [self.tableview registerNib:[UINib nibWithNibName:@"CarDetailPrintCell" bundle:nil] forCellReuseIdentifier:@"CarDetailPrintCell"];
//    [self.tableview registerNib:[UINib nibWithNibName:@"CarDetailPriceCell" bundle:nil] forCellReuseIdentifier:@"CarDetailPriceCell"];
    [self.tableview registerNib:[UINib nibWithNibName:@"CarFloorPriceCell" bundle:nil] forCellReuseIdentifier:@"CarFloorPriceCell"];
    [self.tableview registerNib:[UINib nibWithNibName:@"CardescribeCell" bundle:nil] forCellReuseIdentifier:@"CardescribeCell"];
    [self.tableview registerNib:[UINib nibWithNibName:@"CarCodeCell" bundle:nil] forCellReuseIdentifier:@"CarCodeCell"];
    [self.tableview registerClass:[CarRadarDataCell class] forCellReuseIdentifier:@"CarRadarDataCell"];
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = false;
    }

    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(weakSelf.view);
    }];
    
    if (_imageUrls.count == 0) {
        return;
    }
    self.bannerView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width * 3 / 4) delegate:self placeholderImage:[UIImage imageNamed:@"banner"]];
    self.bannerView.currentPageDotImage = [UIImage imageWithColor:[UIColor clearColor] forSize:CGSizeMake(15, 2)];
    self.bannerView.pageDotImage = [UIImage imageWithColor:[UIColor clearColor] forSize:CGSizeMake(15, 2)];
    self.bannerView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
    self.tableview.tableHeaderView = self.bannerView;
//    self.bannerView.autoScroll = NO;
    [self.view bringSubviewToFront:self.navigationBarView];
    self.bannerView.imageURLStringsGroup = self.imageUrls;
    
    self.bannerL = [[UILabel alloc] initWithFrame:CGRectMake(Screen_width - 65, Screen_width * 3/4 - 40, 50, 25)];
    self.bannerL.layer.masksToBounds = YES;
    self.bannerL.layer.cornerRadius = 25/2;
    self.bannerL .backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.7];
    self.bannerL.textColor = kColorWhite();
    self.bannerL.textAlignment = NSTextAlignmentCenter;
    [self.bannerView addSubview:self.bannerL];
  
    self.bannerL.text = [NSString stringWithFormat:@"1/%zd",_imageUrls.count];

    
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return [tableView fd_heightForCellWithIdentifier:@"CarDetailTopCell" configuration:^(CarDetailTopCell* cell) {
               
                cell.carInfo = _infomodel.result;
                
            }];
        }else if (indexPath.row == 2){
            if (_infomodel.result.carAuthState != 10 && _infomodel.result.carSaleState != 20) {
                return 0;
            }
            if (_isShowCode == NO) {
                return 70;

            }else{
                return 80;
                
            }
        }

        return 290;
        
    }else if (indexPath.section == 1){
        
        return 250;
    }else if (indexPath.section == 2){
        
        
        return [tableView fd_heightForCellWithIdentifier:@"CardescribeCell" configuration:^(CardescribeCell* cell) {
            NSArray *titles = @[@"瑕疵描述",@"卖点描述",@"按揭说明",@"补充说明"];
            cell.index = indexPath.row;
            cell.title = titles[indexPath.row];
            cell.carInfo = _infomodel.result;
            
        }];
  
   
    }
    return 0.00001;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        if (_isShowCode == NO) {
            return 3;
        }
        return 4;
    }
    if (section == 2){
        return 2;
    }
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            CarDetailTopCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CarDetailTopCell"];
            cell.carInfo = _infomodel.result;
            cell.delegate = self;
            cell.nav = self.navigationController;
            
            return cell;
        }else if (indexPath.row == 1){
            CarRadarDataCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CarRadarDataCell"];
            cell.carInfo = _infomodel.result;
            return cell;
       
        }else if (indexPath.row == 2){
            kSelfWeak;
            CarDetailPrintCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CarDetailPrintCell"];
            cell.layer.masksToBounds = YES;
            cell.showBlock = ^(BOOL isShow) {
                weakSelf.isShowCode = isShow;
                [weakSelf.tableview reloadData];
                
            };
            cell.nav = self.navigationController;
            cell.isShow = self.isShowCode;
            cell.carInfo = _infomodel.result;
            
            return cell;
            
        }
        
        CarCodeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CarCodeCell"];
        cell.carInfo = _infomodel.result;
        
        return cell;
        
    }else if (indexPath.section == 1){
        CarFloorPriceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CarFloorPriceCell"];
        cell.carInfo = _infomodel.result;

        return cell;
        
    }
    NSArray *titles = @[@"瑕疵描述",@"卖点描述",@"按揭说明",@"补充说明"];

    CardescribeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CardescribeCell"];
    cell.title = titles[indexPath.row];
    cell.index = indexPath.row;
    cell.carInfo = _infomodel.result;
    
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 1) {

    NSArray *titles = @[@"车况"];
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = UIColorHex(0xf5f5f5);
    UIView *lineview = [[UIView alloc] init];
    lineview.backgroundColor = MAIN_NAV_COLOR();
    [view addSubview:lineview];
    [lineview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(view);
        make.width.mas_offset(2);
    }];
    UILabel *title = [[UILabel alloc] init];
    [view addSubview:title];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view).mas_offset(16);
        make.top.bottom.equalTo(view);
    }];
    title.font = [UIFont systemFontOfSize:13];
    title.textColor = MAIN_FONT_COLOR();
        title.text = titles[section - 1];
   
    return view;
       
    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return 25;
    }
    
    return 0.00001;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    [HUPhotoBrowser showFromImageView:nil withURLStrings:self.norImageUrls placeholderImage:[UIImage imageNamed:@"banner"] atIndex:index dismiss:nil];
    NSLog(@"---点击了第%ld张图片", (long)index);

}
/** 图片滚动回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index{
    
    NSLog(@"---滚到第%ld张图片", (long)index);
    self.bannerL.text = [NSString stringWithFormat:@"%zd/%zd",index + 1,self.imageUrls.count];

}

- (void)carDetailTopCell:(CarDetailTopCell *)cell callBtnDidClick:(UIButton *)sender {
    
    [self.callPhoneWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel://400-150-1999"]]];
}

@end
